package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.UserMapper;
import com.gxa.zhonglong.pojo.User;
import com.gxa.zhonglong.service.UserService;
import com.gxa.zhonglong.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/26 19:57
 * Description:
 * Version: V1.0
 */
@Service
@Transactional(rollbackFor = {Throwable.class})
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Map<String, Object>> selectByName(String name) throws BusinessException {
        final List<Map<String, Object>> maps = userMapper.selectByName(name);
        if (maps.size() == 0) {
            throw new BusinessException(ExceptionCode.NODATA);
        }
        return maps;
    }

    @Override
    public List<Map<String, Object>> selectByPage(Integer page, Integer size,String name) throws BusinessException {
        final List<Map<String, Object>> maps = userMapper.selectByPage((page - 1) * size, size,name);
        if (maps.size() == 0) {
            throw new BusinessException(ExceptionCode.NODATA);
        }
        return maps;
    }

    @Override
    public Integer selectCount(String name) {
        return userMapper.selectCount(name);
    }

    @Override
    public void add(User user) throws BusinessException {
        user.setPwd(MD5Util.encode(user.getPhone().substring(3)));
        if (!userMapper.add(user)) {
            throw new BusinessException(ExceptionCode.ADD_DEFEATED);
        }
    }

    @Override
    public void update(User user) throws BusinessException {
        if (!userMapper.update(user)) {
            throw new BusinessException(ExceptionCode.UPDATE_FALSE);
        }
    }

    @Override
    public void del(List<Long> ids) throws BusinessException {
        if (userMapper.deleteById(ids) < 1) {
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }
    }

    @Override
    public Map<String, Object> login(String userName, String password) throws BusinessException {

        // 最终存储的map容器
        Map<String, Object> map = new HashMap<>();
        // 判断员工是否存在
        Map<String, Object> userMap = userMapper.selectByUserName(userName);
        // 角色权限列表集合
        List<Map<String, Object>> firstList = null;
        if (userMap != null) {

            // 加密密码
            String pwd = MD5Util.encode(password.trim());
            if (userMap.get("user_pwd").equals(pwd)) {
                // 身份校验通过
                Long userId = (Long) userMap.get("user_id");
                String name = (String) userMap.get("user_name");

                // 获得权限列表(firstList)
                firstList = userMapper.getFirstMenuList(userId);

                // 获取商品管理权限列表
                Map<String, Object> goodsMgr = userMapper.getGoodsMgr(userId);
                if (goodsMgr != null) {
                    Long menuId1 = (Long) goodsMgr.get("menu_id");
                    List<Map<String, Object>> secondMenuList = userMapper.getSecondMenuList(userId, menuId1);
                    goodsMgr.put("children", secondMenuList);
                }

                // 遍历移除商品管理权限
                for (int i = 0; i < firstList.size(); i++) {
                    if ((Long)firstList.get(i).get("menu_id") == 7) {
                        firstList.remove(i);
                    }
                }

                // 处理二级权限列表，体现与一级的层级关系(二级list2)
                // roleId：用于查询对应二级权限
                // menuId：用于查询对应二级权限
                Long menuId = null;
                List<Map<String, Object>> secondList = null;
                for (Map<String, Object> firstMap : firstList) {
                    menuId = (Long) firstMap.get("menu_id");
                    secondList = userMapper.getSecondMenuList(userId, menuId);
                    firstMap.put("children", secondList);
                }

                // 存入map
                map.put("commodityManagement", goodsMgr);
                map.put("menu", firstList);
                map.put("id",userId);
                map.put("name",name);
            } else {
                throw new BusinessException(ExceptionCode.LOGIN_FAILED);
            }
        } else {
            // 补充: 自定义异常处理，返回用户名或密码错误提示
            throw new BusinessException(ExceptionCode.LOGIN_FAILED);
        }

        return map;
    }

}
